﻿using System.Collections.Generic;
using System.Linq;
using DTO;

namespace DAO
{
    public class EmailDangKyNhanTinRaoVatDAO
    {
        public static List<EmailDangKyNhanTinRaoVatDTO> getAll()
        {
            List<EmailDangKyNhanTinRaoVatDTO> result = new List<EmailDangKyNhanTinRaoVatDTO>();
            RaoVatDataContext dbRaoVat = new RaoVatDataContext();

            var query = dbRaoVat.EMAILDANGKYNHANTINRAOVATs.Where(dm => dm.Deleted.Equals(0)).ToList();

            foreach (var ds in query)
            {
                EmailDangKyNhanTinRaoVatDTO dto = new EmailDangKyNhanTinRaoVatDTO();

                dto.MaEmailDangKyNhanTinRaoVat = ds.MaEmailDangKyNhanTinRaoVat;
                dto.EmailNhanTinRaoVat = ds.EmailNhanTinRaoVat;
                dto.MaDanhMucCon = ds.MaDanhMucCon.Value;
                dto.ThoiGianDangKy = ds.ThoiGianDangKy.Value;

                result.Add(dto);
            }

            return result;
        }

        public static EmailDangKyNhanTinRaoVatDTO getAlongCode(int maEmail)
        {
            EmailDangKyNhanTinRaoVatDTO result = new EmailDangKyNhanTinRaoVatDTO();
            RaoVatDataContext dbRaoVat = new RaoVatDataContext();

            var query = dbRaoVat.EMAILDANGKYNHANTINRAOVATs.Where(dm => dm.Deleted.Equals(0) &&
                dm.MaEmailDangKyNhanTinRaoVat == maEmail).Single();

            result.MaEmailDangKyNhanTinRaoVat = query.MaEmailDangKyNhanTinRaoVat;
            result.EmailNhanTinRaoVat = query.EmailNhanTinRaoVat;
            result.MaDanhMucCon = query.MaDanhMucCon.Value;
            result.ThoiGianDangKy = query.ThoiGianDangKy.Value;

            return result;
        }

        private static List<EmailDangKyNhanTinRaoVatDTO> LayDSTheoEmail(string strEmail)
        {
            List<EmailDangKyNhanTinRaoVatDTO> result = new List<EmailDangKyNhanTinRaoVatDTO>();
            RaoVatDataContext dbRaoVat = new RaoVatDataContext();

            var query = dbRaoVat.EMAILDANGKYNHANTINRAOVATs.Where(dm => dm.Deleted.Equals(0) &&
                dm.EmailNhanTinRaoVat == strEmail).ToList();

            foreach (var ds in query)
            {
                EmailDangKyNhanTinRaoVatDTO dto = new EmailDangKyNhanTinRaoVatDTO();

                dto.MaEmailDangKyNhanTinRaoVat = ds.MaEmailDangKyNhanTinRaoVat;
                dto.EmailNhanTinRaoVat = ds.EmailNhanTinRaoVat;
                dto.MaDanhMucCon = ds.MaDanhMucCon.Value;
                dto.ThoiGianDangKy = ds.ThoiGianDangKy.Value;

                result.Add(dto);
            }

            return result;
        }

        public static List<EmailDangKyNhanTinRaoVatDTO> LayDSTheoMaDanhMucCon(int madanhmuccon)
        {
            List<EmailDangKyNhanTinRaoVatDTO> result = new List<EmailDangKyNhanTinRaoVatDTO>();
            RaoVatDataContext dbRaoVat = new RaoVatDataContext();

            var query = dbRaoVat.EMAILDANGKYNHANTINRAOVATs.Where(dm => dm.Deleted.Equals(0) &&
                dm.MaDanhMucCon == madanhmuccon).ToList();

            foreach (var ds in query)
            {
                EmailDangKyNhanTinRaoVatDTO dto = new EmailDangKyNhanTinRaoVatDTO();

                dto.MaEmailDangKyNhanTinRaoVat = ds.MaEmailDangKyNhanTinRaoVat;
                dto.EmailNhanTinRaoVat = ds.EmailNhanTinRaoVat;
                dto.MaDanhMucCon = ds.MaDanhMucCon.Value;
                dto.ThoiGianDangKy = ds.ThoiGianDangKy.Value;

                result.Add(dto);
            }

            return result;
        }

        public static bool KiemTraDaDangKyTatCa(string email)
        {
            List<DanhMucConDTO> DSDanhMucCon = DanhMucConDAO.getAll();
            List<EmailDangKyNhanTinRaoVatDTO> DSTheoEmail = LayDSTheoEmail(email);

            if (DSTheoEmail.Count >= DSDanhMucCon.Count)
            {
                return true;
            }

            return false;
        }

        public static bool KiemTraDangKyRoi(string email, int madanhmuccon)
        {
            List<EmailDangKyNhanTinRaoVatDTO> DS = getAll();

            for (int i = 0; i < DS.Count; ++i)
            {
                if (DS[i].EmailNhanTinRaoVat.ToLower().Equals(email.ToLower()) &&
                    DS[i].MaDanhMucCon == madanhmuccon)
                {
                    return true;
                }
            }

            return false;
        }

        public static void Insert(EmailDangKyNhanTinRaoVatDTO _insert)
        {
            RaoVatDataContext dbRaoVat = new RaoVatDataContext();

            EMAILDANGKYNHANTINRAOVAT emaildk = new EMAILDANGKYNHANTINRAOVAT();
            emaildk.EmailNhanTinRaoVat = _insert.EmailNhanTinRaoVat;
            emaildk.MaDanhMucCon = _insert.MaDanhMucCon;
            emaildk.ThoiGianDangKy = _insert.ThoiGianDangKy;
            emaildk.Deleted = _insert.Deleted;

            dbRaoVat.EMAILDANGKYNHANTINRAOVATs.InsertOnSubmit(emaildk);
            dbRaoVat.SubmitChanges();
        }

        public static void Delete(int maEmail)
        {
            RaoVatDataContext dbRaoVat = new RaoVatDataContext();

            EMAILDANGKYNHANTINRAOVAT _delete = (from n in dbRaoVat.EMAILDANGKYNHANTINRAOVATs
                                                where n.MaEmailDangKyNhanTinRaoVat == maEmail && n.Deleted == 0
                                                select n).Single();

            _delete.Deleted = 1;
            dbRaoVat.SubmitChanges();
        }

        public static void Update(EmailDangKyNhanTinRaoVatDTO dto)
        {
            RaoVatDataContext dbRaoVat = new RaoVatDataContext();

            EMAILDANGKYNHANTINRAOVAT _update = (from n in dbRaoVat.EMAILDANGKYNHANTINRAOVATs
                                                where n.MaEmailDangKyNhanTinRaoVat == dto.MaEmailDangKyNhanTinRaoVat && n.Deleted == 0
                                                select n).Single();

            _update.EmailNhanTinRaoVat = dto.EmailNhanTinRaoVat;
            _update.MaDanhMucCon = dto.MaDanhMucCon;
            _update.ThoiGianDangKy = dto.ThoiGianDangKy;

            dbRaoVat.SubmitChanges();
        }
    }
}
